package com.reportgenerate.marketreport.entity;

import com.reportgenerate.common.CommonConstant;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @program: reportgenerate
 * @Description: 接口返回数据格式
 * @author: Mr.Cheng
 * @Email: ccelephant_518@126.com
 * @date: 2023/8/22 19:17
 */
@Data
@NoArgsConstructor
@ApiModel(value="接口返回对象", description="接口返回对象")
public class ResultData<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    @ApiModelProperty(value = "成功标志")
    private boolean success = true;

    @ApiModelProperty(value = "返回代码")
    private Integer code = 0;

    @ApiModelProperty(value = "返回处理消息")
    private String message = "";

    @ApiModelProperty(value = "返回数据对象")
    private T data;

    @ApiModelProperty(value = "时间戳")
    private long timestamp = System.currentTimeMillis();

    public ResultData(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public ResultData<T> success(String message) {
        this.message = message;
        this.code = CommonConstant.SC_OK_200;
        this.success = true;
        return this;
    }

    public static<T> ResultData<T> OK() {
        ResultData<T> r = new ResultData<T>();
        r.setSuccess(true);
        r.setCode(CommonConstant.SC_OK_200);
        return r;
    }

    public static<T> ResultData<T> OK(String msg) {
        ResultData<T> r = new ResultData<T>();
        r.setSuccess(true);
        r.setCode(CommonConstant.SC_OK_200);
        r.setData((T) msg);
        r.setMessage(msg);
        return r;
    }

    public static<T> ResultData<T> OK(T data) {
        ResultData<T> r = new ResultData<T>();
        r.setSuccess(true);
        r.setCode(CommonConstant.SC_OK_200);
        r.setData(data);
        return r;
    }

    public static<T> ResultData<T> OK(String msg, T data) {
        ResultData<T> r = new ResultData<T>();
        r.setSuccess(true);
        r.setCode(CommonConstant.SC_OK_200);
        r.setMessage(msg);
        r.setData(data);
        return r;
    }
}
